package com.sq.partner.manager.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.exception.AppException;
import com.sq.partner.manager.mybatis.model.GatewayTxLog;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.service.IGatewayAppService;
import com.sq.partner.manager.service.IGatewayTransManagerService;
import com.sq.partner.manager.service.IGatewayTxLogService;
import com.sq.partner.manager.util.DateUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

/**
 * 
 * @author duguangwei
 * 交易分析
 */
@Controller
@RequestMapping(value = "/transManagerFlowChart")
public class TransManagerFlowChartController {

	@Resource
	private  IGatewayTransManagerService gatewayTransManagerService;
	
	@Resource
	private  IGatewayTxLogService gatewayTxLogService;
	@Resource
	private IGatewayAppService appService;
	
	@RequestMapping(value = "/transMonitoringMain")
	public String transMonitoringMain(ModelMap model,HttpServletRequest request) throws Exception {
		
		return "transManager/transMonitoringMain";
	}
	
	@RequestMapping("/queryDetialGatewayTxLog")
	public @ResponseBody Page<GatewayTxLog>  queryDetialGatewayTxLog(String appId,String date,HttpSession session) throws Exception {
		Page<GatewayTxLog>  page =new Page<GatewayTxLog> ();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(StrUtil.isEmpty(date)){
			Date d=new Date();
			date=sdf.format(d);
		}
		if(!DateUtil.isDate(date)){
			page.setMessage("日期格式错误");
			page.setResult("error");
			return page;
		}
		GatewayUser gatewayuser=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		Date qdate= sdf.parse(date);
		date=sdf.format(qdate);
		GatewayTxLog gatewayTxLog=new GatewayTxLog();
		gatewayTxLog.setDate(date);
		//gatewayTxLog.setDepts(gatewayuser.getDepts());
		if(null!=gatewayuser && StrUtil.isNotEmpty(gatewayuser.getDepartmentStr())){
			if(gatewayuser.getDepartmentStr().contains(Constants.HQ_BRANCH_CODE)){
				gatewayTxLog.setHqFlag("Y");
			}
		}
		gatewayTxLog.setDeptStr(gatewayuser.getDepartmentStr());
		page.setType(gatewayTxLog);
		return gatewayTransManagerService.getDetialGatewayTxLogVo(page);
	}
	/**
	 * 加载饼图 
	 * @throws AppException 
	 * @throws Exception 
	 * */
	@RequestMapping("/querGatewayTxLog")
	public @ResponseBody Page querGatewayTxLog(String appId,String date,String appName,HttpSession session) throws SQLException, AppException {
		Page page=new Page();
		if(StrUtil.isEmpty(appId)){
			page.setMessage("应用名不能为空");
			page.setResult("error");
			return page;
		}
		GatewayUser gatewayuser=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		//List<DeptDto> depts =gatewayuser.getDepts(); 
		page=gatewayTxLogService.getGatewayTxLog(appId,date,appName,gatewayuser);
		return page;
	}
	/**
	 * 加载数据列表
	 * 根据msg_Id和channel_Code查询不同错误代码的日志数
	 * @throws SQLException 
	 * @throws AppException 
	 * */
	@RequestMapping("/querErrorGatewayTxLog")
	public @ResponseBody Page querErrorGatewayTxLog(String appId,String date,String channelCode,HttpSession session) throws SQLException, AppException {
		Page page=new Page();
		if(StrUtil.isEmpty(date)){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date d=new Date();
			date=sdf.format(d);
		}
		if(StrUtil.isEmpty(appId)){
			
			page.setMessage("应用名不能为空");
			page.setResult("error");
			return page;
		}
		GatewayUser gatewayuser=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		//List<DeptDto> depts =gatewayuser.getDepts(); 

		page=gatewayTxLogService.getErrorGatewayTxLog(appId,channelCode,date,gatewayuser);
		return page;
		
	}
	/**
	 * 加载条形图
	 * @throws Exception 
	 * */
	@RequestMapping("/queryChatDateLoad")
	public @ResponseBody Page queryChatDateLoad(String appId,String date,HttpSession session) throws Exception {
		Page page=new Page();
		if(StrUtil.isEmpty(date)){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date d=new Date();
			date=sdf.format(d);
		}
		
		GatewayUser gatewayuser=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		//List<DeptDto> depts =gatewayuser.getDepts(); 

		if(StrUtil.isNotEmpty(appId)){
			page=gatewayTxLogService.getChatDateLoad(appId,date,gatewayuser);
		}
		return page;
	}
	
}
